# Input:    /Data/finalData.RData
# Author:   JB


rm(list = ls())
require(tidyverse)
require(ggridges)
require(ggrepel)

setwd('C:/Users/Jimbo/Dropbox/FED/FED/Paper/JOP/RR1_replication/')

load('./data/finalData.RData')


forBal <- utterance_level %>%
  mutate(interrupted = ifelse(grepl('--$',textclean),1,0)) %>%
  mutate(interruptor = ifelse(grepl('--$',lag(textclean)),1,0)) %>%
  mutate(date = as.Date(gsub('fed|\\.txt','',docID)),
         anyDaughters = ifelse(nDaughters > 0,1,0),
         dem = (party == 'D')+0,
         male = (gender == 'M')+0,
         fedChairs = ifelse(date < as.Date('2006-01-01'),'Greenspan',
                            ifelse(date < as.Date('2014-01-01'),'Bernanke',
                                   ifelse(date > as.Date('2018-01-01'),'Powell','Yellen'))),
  ) %>%
  select(opensecretsID,date,anyDaughters,fedChairs,dem,
         nominate_dim1,age,seniority,male,votepct,nKids,nSons,nDaughters,
         firstDaughter,docID) %>%
  distinct() %>%
  select(-opensecretsID,-date,-docID)

require(stargazer)
balTab <- RCT::balance_table(forBal %>%
                               mutate(fedChairs = relevel(factor(fedChairs),ref = 'Yellen')),
                             treatment = 'fedChairs') %>%
  rename(variable = variablesBernanke,
         Yellen = Media_controlBernanke,
         Greenspan = Media_tratGreenspan,
         Powell = Media_tratPowell,
         Bernanke = Media_tratBernanke) %>%
  mutate(Bernanke = ifelse(p_valueBernanke < .05,paste0(round(Bernanke,2),'*'),
                           round(Bernanke,2)),
         Greenspan = ifelse(p_valueGreenspan < .05,paste0(round(Greenspan,2),'*'),
                            round(Greenspan,2)),
         Powell = ifelse(p_valuePowell < .05,paste0(round(Powell,2),'*'),
                         round(Powell,2)),
         Yellen = as.character(round(Yellen,2))) %>%
  select(-matches('p_value')) %>%
  mutate(variable = gsub('(.*)(Daughter|Son|Kid)','Family: \\1\\2',
                         gsub('(.*)(age|male|seniority)','Demog: \\1\\2',
                              gsub('(.*)(dem|nominate_dim1|votepct)','Pol: \\1\\2',variable)))) %>%
  arrange(variable) %>%
  select(Variable = variable,Yellen,Greenspan,Bernanke,Powell)

stargazer(balTab,summary = F,out = '../Paper/Tables/balance_table.tex',rownames = F)